<?php
/**
 * @category Goatherd
 * @package Goatherd\Commons
 *
 * @author Copyright (c) 2012 Maik Penz <maik@phpkuh.de>
 * @version $Id: Tree.php 105 2012-06-15 11:00:45Z maik@phpkuh.de $
 *
 * This file is part of Goatherd library.
 *
 * Goatherd library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * Goatherd library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Goatherd library. If not, see <http://www.gnu.org/licenses/>.
 */
namespace Goatherd\Commons\Word;

/**
 * Abstract tree interface.
 *
 * @package Goatherd\Commons
 * @subpackage Word
 */
class Tree
implements ITree
{
    /**
     *
     * @var array
     */
    protected $_root = array(self::NODE_CHILDREN => array());

    /**
     * (non-PHPdoc)
     * @see Goatherd\Commons\Word.ITree::set()
     */
    public function set(array &$path, $data)
    {
        $node = &$this->_root;
        foreach ($path as &$key) {
            if ( !isset($node[self::NODE_CHILDREN][$key]) ) {
                $node[self::NODE_CHILDREN][$key] = array(self::NODE_CHILDREN => array());
            }

            $node = &$node[self::NODE_CHILDREN][$key];
        }

        // TODO may need flag to allow warning if node data exists already.
        $node[self::NODE_DATA] = $data;

        return $this;
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Commons\Word.ITree::get()
     */
    public function &get(array &$path)
    {
        $node = $this->getNode($path);

        return $node && isset($node[self::NODE_DATA]) ? $node[self::NODE_DATA] : null;
    }

    /**
     * Find node by path.
     *
     * @param array $path
     * @return array|boolean - false if path not in tree.
     */
    public function &getNode(array &$path)
    {
        $node = &$this->_root;

        foreach ($path as &$key) {
            if ( !isset($node[self::NODE_CHILDREN][$key]) ) {
                $node = null;
                break ;
            }
            $node = &$node[self::NODE_CHILDREN][$key];
        }

        return $node;
    }

    /**
     * Get nodes as Tree
     *
     * @param array $path
     * @return \Goatherd\Commons\Word\ITree
     */
    public function getNodeAsTree(array &$path)
    {
        $node = &$this->getNode($path);
        $tree = null;

        if ( is_array($node) ) {
            $tree = new static();
            $tree->setRoot($node);
        }

        return $tree;
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Commons\Word.ITree::isLeaf()
     */
    public function isLeaf(array &$path)
    {
        $node = &$this->getNode($path);

        return $node && isset($node[self::NODE_DATA]);
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Commons\Word.ITree::getRoot()
     */
    public function &getRoot()
    {
        return $this->_root;
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Commons\Word.ITree::setRoot()
     */
    public function setRoot(array &$node)
    {
        $this->_root = &$node;
        return $this;
    }
}